package week_1;

import toolPackage.ListNode;

public class day05_week01 {
    public static void main(String[] args) {
        /**
         * Definition for singly-linked list.
         * public class ListNode {
         *     int val;
         *     ListNode next;
         *     ListNode(int x) {
         *         val = x;
         *         next = null;
         *     }
         * }
         */
        //int val; 存入的值
        //ListNode next; 指向下一个地址   的指针
        ListNode $_1=new ListNode(1);
        ListNode $1_2=new ListNode(2);
        ListNode $2_3=new ListNode(3);
        $_1.next=$1_2;
        $1_2.next=$2_3;
        System.out.println(getIntersectionNode($2_3,$1_2));

    }
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //首先判断传入是否有null
        if(headA==null||headB==null)return null;
        //双指针同时遍历
        ListNode Pa=headA;
        ListNode Pb=headB;
        //如果两串链表不相交
            //如果两串链表长度相等,那么将会在遍历完各自链表后在最后的next:null中返回null
            //如果两串链表长度不同,两个标签会先遍历自身链表然后跳转遍历对方链表,在对方的next:null中返回null
        //如果两串链表有相交
            //如果两串链表长度相等,那么将会在遍历到相同指针后返回子链
            //如果两串链表长度不同,两个标签会先遍历自身链表然后跳转遍历对方链表,在对方链表遍历到相同指针后返回子链
        for(;Pa!=Pb;){
            Pa= Pa==null?headB:Pa.next;
            Pb= Pb==null?headA:Pb.next;
        }
        return Pa;
    }
 /*
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //获取两个链表的长度
        ListNode btemp=headA;
        int A=0;
        for(;;){
            if (btemp==null)break;
            A++;
            btemp=btemp.next;
        }
        ListNode stemp=headB;
        int B=0;
        for(;;){
            if ( stemp==null)break;
            B++;
            stemp=stemp.next;
        }
        int difference;
        if (A>=B){
            difference=A-B;
            btemp=headA;
            stemp=headB;
        }else {
            difference=B-A;
            btemp=headB;
            stemp=headA;
        }
        //逐个比较
        for(int i=0;i<difference;i++){
            btemp=btemp.next;
        }
        for (;;){
            if (btemp.next==stemp.next)return btemp;
            if (btemp.next==null)break;
        }
        return null;
    }*/
}
